package com.alarm;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;

import com.gesture.SetGesture;
import com.notification.NotificationSetting;
import com.voice.SetVoice;

//  Toast.makeText(getApplicationContext(), "Delete" + index, Toast.LENGTH_SHORT).show();

public class WakeMeUp extends Activity {
	private final String TAG = "WakeMeUp";

	private ListView mAlarmList;
	private Switch switchSetVoice, switchSetGesture;
	private AlarmListAdapter mAlarmListAdapter;
	private Alarm mCurrentAlarm;
	private NotificationSetting setting = new NotificationSetting();

	private final int NEW_ALARM_ACTIVITY = 0;
	private final int EDIT_ALARM_ACTIVITY = 1;
	private final int PREFERENCES_ACTIVITY = 2;
	private final int ABOUT_ACTIVITY = 3;
	private final int SET_GESTURE = 4;
	private final int SET_VOICE = 5;

	private final int CONTEXT_MENU_EDIT = 0;
	private final int CONTEXT_MENU_DELETE = 1;
	private final int CONTEXT_MENU_DUPLICATE = 2;

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main);

		Log.i(TAG, "WakeMeUp.onCreate()");

		switchSetVoice = (Switch) findViewById(R.id.switch_set_voice);
		switchSetGesture = (Switch) findViewById(R.id.switch_set_gesture);
		mAlarmList = (ListView) findViewById(R.id.alarm_list);
		setting.load();
		refreshSetting();
		
		mAlarmListAdapter = new AlarmListAdapter(this);
		mAlarmList.setAdapter(mAlarmListAdapter);
		mAlarmList.setOnItemClickListener(mListOnItemClickListener);
		registerForContextMenu(mAlarmList);

		mCurrentAlarm = null;
	}

	private void refreshSetting() {
		switchSetVoice.setEnabled(setting.existVoice());
		switchSetGesture.setEnabled(setting.existGesture());
		switchSetVoice.setChecked(setting.isVoice());
		switchSetGesture.setChecked(setting.isGesture());
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(TAG, "WakeMeUp.onDestroy()");
		// mAlarmListAdapter.save();
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.i(TAG, "WakeMeUp.onResume()");
		mAlarmListAdapter.updateAlarms();
	}

	public void onAddAlarmClick(View view) {
		Intent intent = new Intent(getBaseContext(), EditAlarm.class);

		mCurrentAlarm = new Alarm(this);
		mCurrentAlarm.toIntent(intent);

		WakeMeUp.this.startActivityForResult(intent, NEW_ALARM_ACTIVITY);
	}

	public void onSwitchVoiceClick(View view){
		boolean checked = ((Switch)view).isChecked();
		Log.d(TAG, "Voice: " + checked);
		setting.setVoiceOn(checked);
		if (checked){
			setting.setGestureOn(false);
			switchSetGesture.setChecked(false);
		}			
		setting.save();
	}
	public void onSwitchGestureClick(View view){
		boolean checked = ((Switch)view).isChecked();
		Log.d(TAG, "Gesture: " + checked);
		setting.setGestureOn(checked);
		if (checked){
			setting.setVoiceOn(false);
			switchSetVoice.setChecked(false);
		}
		setting.save();
	}
	
 	public void onSetVoiceClick(View view) {
		Intent intent = new Intent(getBaseContext(), SetVoice.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("setting", setting);
		intent.putExtras(bundle);
		startActivityForResult(intent, SET_VOICE);
	}
	public void onSetGestureClick(View view) {
		Intent intent = new Intent(getBaseContext(), SetGesture.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("setting", setting);
		intent.putExtras(bundle);
		startActivityForResult(intent, SET_GESTURE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case NEW_ALARM_ACTIVITY:
			if (resultCode == RESULT_OK) {
				mCurrentAlarm.fromIntent(data);
				mAlarmListAdapter.add(mCurrentAlarm);
			}
			mCurrentAlarm = null;
			break;
		case EDIT_ALARM_ACTIVITY:
			if (resultCode == RESULT_OK) {
				mCurrentAlarm.fromIntent(data);
				mAlarmListAdapter.update(mCurrentAlarm);
			}
			mCurrentAlarm = null;
			break;
		case PREFERENCES_ACTIVITY:
			mAlarmListAdapter.onSettingsUpdated();
			break;
		case SET_GESTURE:
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				setting = (NotificationSetting) bundle.getSerializable("setting");
				refreshSetting();
			}
			break;
		case SET_VOICE:
			if (resultCode == RESULT_OK) {
				Bundle bundle2 = data.getExtras();
				setting = (NotificationSetting) bundle2.getSerializable("setting");
				refreshSetting();
			}
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (R.id.menu_settings == item.getItemId()) {
			Intent intent = new Intent(getBaseContext(), Preferences.class);
			startActivityForResult(intent, PREFERENCES_ACTIVITY);
			return true;
		} else if (R.id.menu_about == item.getItemId()) {
			Intent intent = new Intent(getBaseContext(), About.class);
			startActivity(intent);
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.alarm_list) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

			menu.setHeaderTitle(mAlarmListAdapter.getItem(info.position)
					.getTitle());
			menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
			menu.add(Menu.NONE, CONTEXT_MENU_DELETE, Menu.NONE, "Delete");
			menu.add(Menu.NONE, CONTEXT_MENU_DUPLICATE, Menu.NONE, "Duplicate");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int index = item.getItemId();

		if (index == CONTEXT_MENU_EDIT) {
			Intent intent = new Intent(getBaseContext(), EditAlarm.class);

			mCurrentAlarm = mAlarmListAdapter.getItem(info.position);
			mCurrentAlarm.toIntent(intent);
			startActivityForResult(intent, EDIT_ALARM_ACTIVITY);
		} else if (index == CONTEXT_MENU_DELETE) {
			mAlarmListAdapter.delete(info.position);
		} else if (index == CONTEXT_MENU_DUPLICATE) {
			Alarm alarm = mAlarmListAdapter.getItem(info.position);
			Alarm newAlarm = new Alarm(this);
			Intent intent = new Intent();

			alarm.toIntent(intent);
			newAlarm.fromIntent(intent);
			newAlarm.setTitle(alarm.getTitle() + " (copy)");
			mAlarmListAdapter.add(newAlarm);
		}

		return true;
	}

	private AdapterView.OnItemClickListener mListOnItemClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(getBaseContext(), EditAlarm.class);

			mCurrentAlarm = mAlarmListAdapter.getItem(position);
			mCurrentAlarm.toIntent(intent);
			WakeMeUp.this.startActivityForResult(intent, EDIT_ALARM_ACTIVITY);
		}
	};

}
